package com.whacos.modules.oa.controller;

import com.whacos.common.controller.BaseController;
import com.whacos.common.vo.ResponseVO;
import com.whacos.modules.oa.entity.Message;
import com.whacos.modules.oa.entity.ResponseMessage;
import com.whacos.modules.system.service.SessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.security.Principal;

/**
 * @author xiongdun
 * @description 通知通告
 * @since 2018/11/17 17:11
 */
@Controller
@RequestMapping(value = "/oa/webSocket")
public class WebSocketController extends BaseController {

    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    @Autowired
    private SessionService sessionService;

//	@Autowired
//    WelcomeTask welcomeTask;

	@MessageMapping("/welcome") // 浏览器发送请求通过@messageMapping 映射/welcome 这个地址。
	@SendTo("/topic/getResponse") // 服务器端有消息时,会订阅@SendTo 中的路径的浏览器发送消息。
	@PostMapping("/topic/getResponse")
    public ResponseMessage say(Message message) throws Exception {
		Thread.sleep(1000);
		return new ResponseMessage("Welcome, " + message.getName() + "!");
	}

	@GetMapping("/test")
	public String test() {
		return "test";
	}

	@RequestMapping("/welcome")
	@ResponseBody
	public ResponseVO say02() {
		try {
			// welcomeTask.sayWelcome();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseVO.success();
	}

    @ResponseBody
    @GetMapping("/chat")
    public String  handleChat(Principal principal, String msg) {
        /*template.convertAndSendToUser(sessionService.listPrincipal().get(0).toString(), "/queue/notifications", principal.getName() + "给您发来了消息：" + msg);
        return sessionService.listPrincipal().get(0).toString();*/
        return "chat";
    }
}